﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Text;
using System.Configuration;

using BizElements.BusinessLayer;

namespace BizElements.Web
{
    /// <summary>
    /// <see cref="DataGrid"/> column which displays the row number of the current item.
    /// </summary>
    public class RowNumberColumn : TemplateColumn
    {
        #region RowNumberTemplate class.

        /// <summary>
        /// <see cref="DataGrid"/> template which displays the row number of the current item.
        /// </summary>
        private class RowNumberTemplate : ITemplate
        {
            #region FieldPermissions.

            Unit width;
            string cssClass;

            #endregion

            #region Constructors.

            /// <summary>
            /// Initializes a new instance of the <see cref="RowNumberTemplate"/> class.
            /// </summary>
            public RowNumberTemplate()
            { }

            /// <summary>
            /// Initializes a new instance of the <see cref="RowNumberTemplate"/> class.
            /// </summary>
            /// <param name="width">The width of the label used in the template.</param>
            /// <param name="cssClass">CSS Class name applied to label created by the template.</param>
            public RowNumberTemplate(int width, string cssClass)
            {
                this.width = width;
                this.cssClass = cssClass;
            }

            #endregion

            #region Methods.

            /// <summary>
            /// Defines the <see cref="DataGrid"/> that child controls and the template belong to.
            /// </summary>
            /// <param name="container">The <b>TableCell</b> which contains the controls instantiated by this template.</param>
            public void InstantiateIn(Control container)
            {
                Label label = new Label();
                label.DataBinding += new EventHandler(this.BindRowNum);
                container.Controls.Add(label);
            }

            private void BindRowNum(object sender, EventArgs e)
            {
                Label label = (Label)sender;
                ApplyStyle(label);

                DataGridItem currentItem = (DataGridItem)label.NamingContainer;
                int rownum = currentItem.DataSetIndex + 1;
                label.Text = rownum.ToString() + ".";
            }

            /// <summary>
            /// Applies CSS slass and width to a <see cref="WebControl"/>.
            /// </summary>
            /// <param name="control">Control to which the style is applied.</param>
            protected void ApplyStyle(WebControl control)
            {
                if (this.cssClass != null)
                    control.CssClass = this.cssClass;
                if (this.width.Value > 0)
                    control.Width = this.width;
            }

            #endregion

            #region Properties.

            /// <summary>
            /// Gets or sets the CSS Class name applied to controls created by the template.
            /// </summary>
            public string CssClass
            {
                get { return this.cssClass; }
                set { this.cssClass = value; }
            }

            /// <summary>
            /// Gets or sets the width of the control used in the template.
            /// </summary>
            public Unit Width
            {
                get { return this.width; }
                set { this.width = value; }
            }

            #endregion
        }

        #endregion

        #region Constructors.

        /// <summary>
        /// Initializes a new instance of the <see cref="RowNumberColumn"/> class.
        /// </summary>
        public RowNumberColumn()
            : this("")
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="RowNumberColumn"/> class.
        /// </summary>
        /// <param name="headerText">Text displayed in the header section of the column.</param>
        public RowNumberColumn(string headerText)
        {
            this.EditItemTemplate = new RowNumberTemplate();
            this.ItemTemplate = new RowNumberTemplate();
            this.ItemStyle.Wrap = false;
            this.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
            this.HeaderText = headerText;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="RowNumberColumn"/> class.
        /// </summary>
        /// <param name="headerText">Text displayed in the header section of the column.</param>
        /// <param name="width">The width of the column.</param>
        /// <param name="cssClass">CSS Class name applied to column.</param>
        public RowNumberColumn(string headerText, int width, string cssClass)
        {
            this.EditItemTemplate = new RowNumberTemplate(width, cssClass);
            this.ItemTemplate = new RowNumberTemplate(width, cssClass);
            this.HeaderText = headerText;
            this.ItemStyle.Wrap = false;
            this.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
        }

        #endregion

        #region Overriden public property attributes - for compliance with "Generate Local Resource" tool.

        /// <summary>
        /// Gets or sets the template for displaying the item selected for editing in a TemplateColumn object.
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override ITemplate EditItemTemplate
        {
            get { return base.EditItemTemplate; }
            set { base.EditItemTemplate = value; }
        }

        /// <summary>
        /// Gets or sets the template for displaying a data item in a TemplateColumn object.
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override ITemplate ItemTemplate
        {
            get { return base.ItemTemplate; }
            set { base.ItemTemplate = value; }
        }

        #endregion
    }
}